package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.SceneCollect;
import com.jiantong.lawEnforcing.service.SceneCollectService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 影音记录
 * Created by mz03 on 2017/8/25.
 */
@Slf4j
@OpenBean
public class SceneCollectApis {
    @RpcConsumer
    private SceneCollectService sceneCollectService;


    /**
     * 根据cases_basic_id查询一条信息
     * @param cases_basic_id
     * @return
     */
    @OpenMethod(
            key = "find.sceneCollect.by.cases_basic_id",
            paramNames = {"cases_basic_id"},
            httpMethods = RequestMethod.GET
    )
    public SceneCollect findByCases_basic_id(Long cases_basic_id){
        Response<SceneCollect> response = sceneCollectService.findByCases_basic_id(cases_basic_id);
        if (!response.isSuccess()){
            log.error("failed to find sceneCollect read by findByCases_basic_id = {}, cause : {}", cases_basic_id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.sceneCollect.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public SceneCollect findById(Long id){
        Response<SceneCollect> response = sceneCollectService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find sceneCollect read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param sceneCollect
     * @return
     */
    @OpenMethod(
            key = "create.sceneCollect",
            paramNames = {"sceneCollect"},
            httpMethods = RequestMethod.POST
    )
    public Long create(SceneCollect sceneCollect){
        Response<Long> response = sceneCollectService.create(sceneCollect);
        if(!response.isSuccess()){
            log.error("failed to create sceneCollect read by sceneCollect = {}, cause : {}", sceneCollect, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.sceneCollect",
            paramNames = {"sceneCollect"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(SceneCollect sceneCollect){
        if(sceneCollect.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = sceneCollectService.update(sceneCollect);
        if(!response.isSuccess()){
            log.error("failed to update sceneCollect");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update sceneCollect");
        }
        return response.getResult();
    }

    /**
     * 根据案件id分页查询音影信息
     * @param pageNo
     * @param pageSize
     * @param casesBasicId
     * @return
     */
    @OpenMethod(
            key = "find.paging.sceneCollect",
            paramNames = {"pageNo","pageSize","casesBasicId"},
            httpMethods = RequestMethod.GET
    )
    public Paging<SceneCollect> pagingSceneCollect(Integer pageNo, Integer pageSize, Long casesBasicId){
/*        if(status == null){
            log.error("status is null");
            throw new OPClientException("status is null");
        }*/
        Response<Paging<SceneCollect>> pagingResponse =sceneCollectService.pagingByCasesBasicId(pageNo,pageSize,casesBasicId);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging sceneCollect casesBasicId {}, cause : {}", casesBasicId, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }
}
